<?php

/**
 * This is the model class for table "ab_customer_contacts".
 *
 * The followings are the available columns in table 'ab_customer_contacts':
 * @property integer $contact_id
 * @property string $contact_name
 * @property string $contact_email
 * @property string $contact_phone
 * @property string $contact_password
 * @property string $contact_date
 * @property string $contact_add
 * @property integer $customer_id
 * @property integer $status
 */
class CustomerContacts extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return CustomerContacts the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ab_customer_contacts';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('customer_id', 'required'),
			array('customer_id, status', 'numerical', 'integerOnly'=>true),
			array('contact_name, contact_phone', 'length', 'max'=>100),
			array('contact_email', 'length', 'max'=>50),
			array('contact_password', 'length', 'max'=>32),
			array('contact_add', 'length', 'max'=>255),
			array('contact_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('contact_id, contact_name, contact_email, contact_phone, contact_password, contact_date, contact_add, customer_id, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		'customer'=>array(self::HAS_ONE,'Customers','customer_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'contact_id' => '联系人编号',
			'contact_name' => '姓名',
			'contact_email' => '电子邮件',
			'contact_phone' => '手机/座机',
			'contact_password' => '登录密码',
			'contact_date' => '创建日期',
			'contact_add' => '联系地址',
			'customer_id' => '所属客户帐户',
			'status' => '当前状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('contact_id',$this->contact_id);
		$criteria->compare('contact_name',$this->contact_name,true);
		$criteria->compare('contact_email',$this->contact_email,true);
		$criteria->compare('contact_phone',$this->contact_phone,true);
		$criteria->compare('contact_password',$this->contact_password,true);
		$criteria->compare('contact_date',$this->contact_date,true);
		$criteria->compare('contact_add',$this->contact_add,true);
		$criteria->compare('customer_id',$this->customer_id);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getContactsStatus()
	{
		return array('0'=>'锁定','1'=>'活动');
	}
	public function getContactsStatusText()
	{
		$array = $this->getContactsStatus();
		return $array[$this->status];
	}
	public function getRelatedCustomer()
	{
		$c_id = $this->customer_id;
		$c = Customers::model()->findByPk($c_id);
		return $c;
	}
}